package com.blank.white.common.exception;


import com.blank.white.common.exception.annoation.IUnThrowException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;


/**
 * @desc 自定义异常基础类
 * @classname BaseException
 */
public class BaseException extends RuntimeException implements IUnThrowException, Serializable {

    private final ErrorsEnum error;

    private String message;

    public BaseException(ErrorsEnum error) {
        this.error = error;
    }


    public BaseException(ErrorsEnum error, String message) {
        super(error.getMessage());
        this.error = error;
        if (!ObjectUtils.isEmpty(message)) {
            this.message = message;
        }
    }

    public BaseException(ErrorsEnum error, Throwable throwable) {
        super(error.getMessage(), throwable);
        this.error = error;
    }

    public BaseException addMessage(String message) {
        this.message = message;
        return this;
    }

    public String getMsg() {
        if (StringUtils.isNotBlank(message)) {
            return message;
        }
        return this.error.getMessage();
    }

    protected BaseException(ErrorsEnum error, String data, Throwable cause) {
        super(error.getMessage(), cause);
        this.error = error;
        if (!ObjectUtils.isEmpty(data)) {
            this.message = data;
        }
    }

    public ErrorsEnum getError() {
        return error;
    }

    public String getData() {
        return message;
    }

    @Override
    public String toString() {
        String message = super.toString() +
                ", error=" + error +
                ", message=" + this.message;
        return message;
    }
}
